<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class Table extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\TableModel();
        $this->validate=new \app\admin\validate\TableValidate();
    }  
    public function index(){
        $listRows=Request::param("listRow")?Request::param("listRow"):config("app.page_show_line");//每页显示多少条
        $data=$this->model->order('sort asc,table_id desc')->paginate(['list_rows'=>$listRows,'query' => request()->param()]);
        // 获取分页显示
        $pagelist = $data->render();
        View::assign("data",$data);
        View::assign("pagelist",$pagelist);
        return View::fetch();
    }
    //更新排序
    public function updateSort(){
        $table_id=Request::param('table_id');
        $sort=Request::param('sort');
        foreach($table_id as $key=>$v){
            $this->model->update(['table_id'=>$table_id[$key],'sort'=>$sort[$key]]);
        }
        $this->success("设置成功");
    }    
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $this->model->save($data);
            $lastId=$this->model->table_id;
            $this->model->createTable();//创建数据表
            $this->model->createTableFieldHtml();//创建htmlcode
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            $this->success("添加成功");
        }else{
            return View::fetch();
        }
    }
    public function update(){        
        if(request()->isPost()){
            $data=$this->request->param();
            //编辑状态下重置规则
            $this->validate->rule['name']['unique']='table,name,'.Request::param('table_id',0,"intval");    
            $this->validate->rule['table_name']['unique']='table,table_name,'.Request::param('table_id',0,"intval"); 
            $this->validate->append('table_id','require');
            $this->validate->remove('table_name','checkTable');
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            //更新表
            $this->model->updateTable($data);
            //
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data["table_id"],$this->request->param());
            $this->success($data);
         
        }else{
            $table_id=Request::param('table_id');
            $r=$this->model->find($table_id);
            View::assign("r",$r);
            return View::fetch();
        }
    }
    //删除数据表
    public function delete(){
        $table_id=Request::param('table_id');
        if (empty($table_id)){
            $this->error("id不存在!");
        }        
        $data = $this->model->find($table_id);
        if (!$data) {
            $this->error("数据表不存在!");
        }
        //检查该表下是否有模型，有模型禁止删除（防止误操作）
        $model = Db::name("model")->where("table_id",$table_id)->find();
        if($model){
            $this->error("该数据表下有模型，请先删除模型!");
        }  
        //检查新表名是否存在（如果存在需要验证该表里是否有数据）
        if (table_exists("cms_" . $data['table_name'])){
            $count=Db::name("cms_".$data['table_name'])->count();
            if($count){
                $this->error("该模型下有数据，请先删除数据！");
            }
        }
        //删除记录
        $this->model->where("table_id",$table_id)->delete();
        //删除所有和这个表相关的字段
        Db::name("table_field")->where("table_id",$table_id)->delete();
        //删除主表
        $this->model->drop_table("cms_".$data['table_name']);
        //删除副表
        $this->model->drop_table("cms_".$data['table_name'] . "_data");        
        //
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$table_id,$this->request->param());
        $this->success("删除成功");
    }
  
}